/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Keyword;

/**
 * An instance of KeywordDAO is a data access object used to access Keyword,
 * which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class KeywordDAO extends BaseDAO {

	public Keyword findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Keyword instance = (Keyword) session.get(Keyword.class.getName(), id);

		return instance;

	}

	public List<Keyword> findByProperty(String propertyName, Object value,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? order by model.name");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Keyword> results = queryObject.list();
		return results;

	}

	public List<Keyword> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? order by model.name");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<Keyword> results = queryObject.list();

		return results;

	}

	@SuppressWarnings("unchecked")
	public List<Keyword> findBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? and ");
		sqlStringBuffer.append(propertyName2);
		sqlStringBuffer.append("=? order by model.name");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		List<Keyword> results = queryObject.list();
		return results;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append("= ? ");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long searchSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" like '");
		sqlStringBuffer.append(value);
		sqlStringBuffer.append("%'");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		Object results = queryObject.uniqueResult();
		return (results == null) ? 0 : ((Number) results).longValue();

	}

	@SuppressWarnings("rawtypes")
	public List searchByProperty(String propertyName, Object value, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select name,gid,tid from gt_keyword where  ");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" like '");
		sqlStringBuffer.append(value);
		sqlStringBuffer.append("%' order by  name");
		Query queryObject = session.createSQLQuery(sqlStringBuffer.toString());
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List results = queryObject.list();
		return results;

	}

	public List<Keyword> findAll(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(Keyword.class.getSimpleName());

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Keyword> results = queryObject.list();

		return results;

	}

}